/*
 * Copyright 2011 James Talmage
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.googlecode.genericsutils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import static junit.framework.Assert.fail;

/**
 * User: jamestalmage
 * Date: 6/13/11
 * Time: 12:05 AM
 */
public abstract class TypeLiteral<T> {
    public TypeLiteral() {
        if(TypeLiteral.class.equals(getClass()))
            throw new IllegalArgumentException("You must extend using anonymous classes - just add {}");
    }

    public Type getType(){
        Type type =  GenericTypeUtils.getGenericTypeArgument(TypeLiteral.class,getClass(),0);
        if(type instanceof ParameterizedType && WC.class.equals(((ParameterizedType) type).getRawType())){
            return GenericTypeUtils.getGenericTypeArgument(WC.class,type,0);
        }
        return type;
    }
}
